define(function(require,exports,module){
	var Form = require("form");
	var Dialog = require("dialog");
	var service = require("../store/roomTypeSvc");
	
	function checkFinish(i){
		if(i == 6){
			return true;
		}else{
			return false;
		}
	}
	
	module.exports = {
		initComponent: function(widget){
			var form = new Form({
				parentNode: ".J-form",
				model: {
					id:"form",
					layout: "3_1",
					saveaction: function(){
						Dialog.alert({
							content: "正在保存并上传图片，请稍后……",
							defaultButton: false
						});
						var formData = $("#form").serialize();
						formData += "&servicePoint=" 
							+ widget.get("subnav").getValue("svcPoint");
						var attachmentUrl = "";
						for(var i=1;i<7;i++){
							attachmentUrl += ((form.$("div[el-data-component=attachmentUrl"
									+i+"]").find("input[type=file]").val() || "") + ";");
						}
						formData += "&attachmentUrl=" + attachmentUrl.substring(0,attachmentUrl.length-1);
						service.save(formData,function(data){
							var pk = data.pkRoomType;
							var version = data.version;
							form.upload("attachmentUrl1","api/attachment/roomtype/"+pk+"/1");
							form.upload("attachmentUrl2","api/attachment/roomtype/"+pk+"/2");
							form.upload("attachmentUrl3","api/attachment/roomtype/"+pk+"/3");
							form.upload("attachmentUrl4","api/attachment/roomtype/"+pk+"/4");
							form.upload("attachmentUrl5","api/attachment/roomtype/"+pk+"/5");
							form.upload("attachmentUrl6","api/attachment/roomtype/"+pk+"/6");
							widget.get("grid").refresh();
							//TODO:为上传图片提供充足的时间
							window.setTimeout(function(){
								Dialog.close();
								widget.toList();
							},10000)
						},function(){
							Dialog.close();
						});
					},
					cancelaction: function(){
						widget.toList();
					},
					items: [{
						name: "pkRoomType",
						type: "hidden"
					},{
						name: "version",
						type: "hidden"
					},{
						name: "code",
						label: "房型编号",
						validate: ["required"]
					},{
						name: "name",
						label: "房型名称",
						validate: ["required"]
					},{
						name: "orgBuildings",
						label: "所属楼宇",
						type: "select",
						lazy: true,
						multi: true,
						url: "api/orgbuilding/query",
						params: function(){
							return {
								"servicePoint": widget.get("subnav").getValue("svcPoint")
							}
						},
						keyField: "pkOrgBuilding",
						valueField: "name",
						validate: ["required"]
					},{
						name: "attachmentUrl1",
						type: "file",
						label: "房间照片1"
					},{
						name: "attachmentUrl2",
						type: "file",
						label: "房间照片2"
					},{
						name: "attachmentUrl3",
						type: "file",
						label: "房间照片3"
					},{
						name: "attachmentUrl4",
						type: "file",
						label: "房间照片4"
					},{
						name: "attachmentUrl5",
						type: "file",
						label: "房间照片5"
					},{
						name: "attachmentUrl6",
						type: "file",
						label: "房间照片6"
					}]
				}
			});
			widget.set("form",form);
			$(":button.btn.btn-danger.J-file-btn").width(35);
		}
	};
});